/*
 * Class: Gettor
 * 
 * Created on Sep 26, 2013
 * 
 * (c) Copyright by Minh Tran
 * Global Cybersoft VN.
 * Floor 3-4-5, Helios Building, Quang Trung Software City
 */
package com.minh.tran.sg;

import java.io.IOException;
import java.util.List;

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.w3c.dom.Element;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
import org.xml.sax.SAXException;

import com.minh.tran.sg.model.ISItem;
import com.minh.tran.sg.model.Song;
import com.minh.tran.sg.view.SGettor;

/**
 * The Class Gettor.
 */
public class Gettor extends Thread
{

    /** The Constant LOG. */
    private static final Log LOG = LogFactory.getLog(Gettor.class);

    /** The song. */
    private final ISItem song;

    private final List<Song> lstSongs;

    private final Configuration config;

    private boolean isRunning;

    private final SGettor parent;

    /**
     * Instantiates a new gettor.
     *
     * @param song the song
     */
    public Gettor(SGettor parent, final ISItem song, final List<Song> lstSongs, Configuration config)
    {
        this.parent = parent;
        this.song = song;
        this.lstSongs = lstSongs;
        this.config = config;
    }

    /**
     * Start process.
     */
    public void startProcess()
    {
        this.start();
    }

    /**
     * {@inheritDoc}
     * 
     * @see java.lang.Thread#run()
     */
    @Override
    public void run()
    {
        String downloadLink = this.song.getDownloadLink(this.config);
        LOG.info(downloadLink);
        if (downloadLink != null)
        {
            this.getPlayList(downloadLink);
        }
        else
        {
            this.parent.cantGetConnectionToLink(this.song.getInfo());
        }
    }

    /**
     * Gets the play list.
     * @param downloadLink 
     *
     * @return the play list
     */
    private void getPlayList(String downloadLink)
    {
        this.isRunning = true;
        try
        {
            DocumentBuilderFactory dbFactory = DocumentBuilderFactory.newInstance();
            DocumentBuilder dBuilder = dbFactory.newDocumentBuilder();

            org.w3c.dom.Document doc = dBuilder.parse(downloadLink);
            doc.getDocumentElement().normalize();
            NodeList nList = doc.getElementsByTagName(Constant.ELE_TRACK);
            for (int temp = 0; temp < nList.getLength(); temp++)
            {
                Node nNode = nList.item(temp);
                if (nNode.getNodeType() == Node.ELEMENT_NODE)
                {
                    Element eElement = (Element) nNode;
                    String info = this.process(eElement.getElementsByTagName(Constant.ELE_INFO).item(0)
                        .getTextContent());
                    if (info != null)
                    {
                        Song song = new Song(info);
                        song.setTitle(this.process(eElement.getElementsByTagName(Constant.ELE_TITLE).item(0)
                            .getTextContent()));
                        song.setLocation(this.process(eElement.getElementsByTagName(Constant.ELE_LOC).item(0)
                            .getTextContent()));
                        song.setImage(this.process(eElement.getElementsByTagName(Constant.ELE_IMG).item(0)
                            .getTextContent()));
                        song.setSinger(this.process(eElement.getElementsByTagName(Constant.ELE_SINGER).item(0)
                            .getTextContent()));
                        this.lstSongs.add(song);
                    }

                }
            }
            this.isRunning = false;
            this.parent.fill();
        }
        catch (ParserConfigurationException e)
        {
            e.printStackTrace();
        }
        catch (SAXException e)
        {
            e.printStackTrace();
        }
        catch (IOException e)
        {
            e.printStackTrace();
        }

    }

    /**
     * Checks if is running.
     *
     * @return true, if is running
     */
    public boolean isRunning()
    {
        return this.isRunning;
    }

    /**
     * Process.
     *
     * @param textContent the text content
     * @return the string
     */
    private String process(String textContent)
    {
        String temp = textContent;
        while (temp.contains(Constant.NEW_LINE))
        {
            temp = temp.replace(Constant.NEW_LINE, Constant.CHAR_EMPTY);
        }
        return temp.trim();
    }
}
